package com.atguigu.gulimall.coupon.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.atguigu.gulimall.coupon.vo.SeckillSessionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.coupon.entity.SeckillSessionEntity;
import com.atguigu.gulimall.coupon.service.SeckillSessionService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 秒杀活动场次
 *
 * @author mayp
 * @email 1582240996@qq.com
 * @date 2020-06-06 08:36:34
 */
@RestController
@RequestMapping("coupon/seckillsession")
public class SeckillSessionController {
    @Autowired
    private SeckillSessionService seckillSessionService;

    /**
     * 查询三天秒杀商品活动
     * @return
     */
    @GetMapping("/laess3DaySession")
    public R getLaess3DaySession(){
        List<SeckillSessionEntity> sessionEntities = seckillSessionService.getLaess3DaySession();
        return R.ok().setData(sessionEntities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = seckillSessionService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("coupon:seckillsession:info")
    public R info(@PathVariable("id") Long id){
		SeckillSessionEntity seckillSession = seckillSessionService.getById(id);

        return R.ok().put("seckillSession", seckillSession);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SeckillSessionVo seckillSession){
        SeckillSessionEntity seckillSessionEntity = new SeckillSessionEntity();
        seckillSessionEntity.setStartTime(dealDateFormat(seckillSession.getStartTime()));
        seckillSessionEntity.setEndTime(dealDateFormat(seckillSession.getEndTime()));
        seckillSessionEntity.setCreateTime(dealDateFormat(seckillSession.getCreateTime()));
        seckillSessionEntity.setId(seckillSession.getId());
        seckillSessionEntity.setName(seckillSession.getName());
        seckillSessionEntity.setStatus(seckillSession.getStatus());
        seckillSessionEntity.setRelationSkus(seckillSession.getRelationSkus());
		seckillSessionService.save(seckillSessionEntity);
        return R.ok();
    }

    public Date dealDateFormat(String oldDate) {
        Date date1 = null;
        DateFormat df2 = null;
        try {
            oldDate= oldDate.replace("Z", " UTC");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
            Date date = df.parse(oldDate);
            SimpleDateFormat df1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
            date1 = df1.parse(date.toString());
            df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        return df2.format(date1);
        return date1;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SeckillSessionEntity seckillSession){
		seckillSessionService.updateById(seckillSession);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		seckillSessionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
